<?php

header("content-type:text/html;charset=utf8");
date_default_timezone_set('PRC');
// $serverName = "localhost";
// $userName = "root";
// $pwd = "root";
// $dbName = "materialadmin ";
//如果连接上数据库后，改变了数据库的结构，会造成下次连接的时候数据库连接不上，解决方法就是重启服务器
$conn = new mysqli('localhost', 'root', 'root', 'materialadmin');

//保证查询出来的中文不会出现乱码
mysqli_set_charset($conn, "utf8");

if ($conn->connect_error) {
    die("连接失败：" . $conn->connect_error);
};


include "./inc.setToken.php";

// 接收管理员 密码 邮箱
$username = $_GET["usersname"];
$pass = $_GET["pass"];
$email = $_GET["email"];

/* 用户名是否存在 */
$sql2 = "select * from login where username='{$username}'";
$result1 = $conn->query($sql2);
/* 如果存在 */
if ($result1->num_rows > 0) {
    $data['code'] = 3003;
    $data["msg"] = '此用户名已被注册';
} else {
    //邮箱是否存在
    $sql = "select * from login where email='{$email}'";
    $result = $conn->query($sql);
    // echo $result;
    //如果管理员存在
    if ($result->num_rows > 0) {
        // 用户名被注册
        $data['code'] = 3002;
        $data["msg"] = '此邮箱已注册';
    } else {
        $sql1 = "insert into login (username,pass,email) values ('$username','$pass','$email')";
        if ($conn->query($sql1) === TRUE) {
            /* 注册成功 */
            $data['code'] = 3000;
            $data["msg"] = '注册成功';
        } else {
            //服务器错误
            $data['code'] = 3001;
            $data["msg"] = '未知错误';
        }
    }
}
echo json_encode($data);
